package IO流.image;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

public class imgString {
    public static void main(String[] args) {
        var w=800;
        var h=800;
        var t=2;
    BufferedImage i = new BufferedImage(w,h,t);
        Graphics2D g = i.createGraphics();
        //设置线条和文件 抗锯齿（平滑)
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
        //背景颜色效果
        var rand=new Random();
        var bac=new Color(rand.nextInt(256),rand.nextInt(256),rand.nextInt(256));
        g.setColor(bac);
        g.fillRect(0,0,w,h);
        //文本字符串
        g.setColor(Color.RED);
        String text="hello world";
        //设置文字的大小
        var font=new Font("宋体",Font.BOLD,100);
        g.setFont(font);
        g.drawString(text,100,100);

        //关闭画笔
        g.dispose();


        try {
            ImageIO.write(i,"png",new File("i.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //打开图像
        try {
            Runtime.getRuntime().exec("cmd /k start i.png");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
